// -----------------------------------------------------------------------
//  <copyright file="OpenIddictRequestExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-09-08 16:09</last-date>
// -----------------------------------------------------------------------

using Liuliu.Fsb.Services.Identity.Application.Identity.Users.Commands;

using OpenIddict.Abstractions;


namespace Liuliu.Fsb.Services.Identity.Application.Identity;

public static class OpenIddictRequestExtensions
{
    /// <summary>
    /// 转换为 <see cref="LoginCommand"/>
    /// </summary>
    public static LoginCommand ToLoginCommand(this OpenIddictRequest request)
    {
        var command = new LoginCommand()
        {
            Account = request.Username!,
            Password = request.Password,
            LoginType = request.TryGetParameter(nameof(LoginCommand.LoginType), out var value0)
                ? value0.Value.CastTo<LoginType>()
                : LoginType.Password,
            IsRemember = request.TryGetParameter(nameof(LoginCommand.IsRemember), out var value1)
                && value1.Value.CastTo<bool>(),
            CaptchaId = request.TryGetParameter(nameof(LoginCommand.CaptchaId), out var value2)
                ? value2.Value.CastTo<string>()
                : null,
            Captcha = request.TryGetParameter(nameof(LoginCommand.Captcha), out var value3)
                ? value3.Value.CastTo<string>()
                : null
        };
        return command;
    }
}
